package com.dgut.lotterySystem.dao.mapper;

import com.dgut.lotterySystem.dao.dataObject.Encrypt;
import com.dgut.lotterySystem.dao.dataObject.UserDO;
import com.dgut.lotterySystem.dao.dataObject.UserInfoDO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select count(*) from user where email=#{email}")
    int countByMail(@Param("email")String mail);

    @Select("select count(*) from user where phone_Number=#{phoneNumber}")
    int countByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    @Insert("insert into user (user_name,email,phone_number,salt,password,identity) values (#{userName},#{email},#{phoneNumber},#{salt},#{password},#{identity})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    int insert(UserDO userDO);

    @Select("select * from user where phone_number=#{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber")Encrypt encrypt);

    @Select("select * from user where email=#{email}")
    UserDO selectByEmail(@Param("email")String email);
    @Select("<script> " +
            "select id,user_name,email,phone_number,salt,password,identity " +
            "from user" +
            " <if test=\"identity!=null\"> " +
            "where identity=#{identity} " +
            "</if> " +
            "order by id desc " +
            "</script>")
    List<UserInfoDO> selectUserInfoListByIdentity(@Param("identity") String identity);

    @Select("<script> " +
            "select id from user " +
            "where id in " +
            "<foreach item='item' collection='userIds' open='(' separator=',' close=')'> " +
            "#{item} " +
            "</foreach> " +
            "</script>")
    List<Long> selectIdsExistsByIds(List<Long> userIds);

    @Select("<script> " +
            "select * from user " +
            "where id in " +
            "<foreach item='item' collection='userIds' open='(' separator=',' close=')'> " +
            "#{item} " +
            "</foreach> " +
            "</script>")
    List<UserDO> batchSelectByIds(List<Long> userIds);




}
